<?php


namespace app\index\business;


use app\common\model\ConsignorReplenish;
use app\common\model\LogisticsReplenish;
use app\common\model\Member;
use app\common\model\MemberInfo;

class memberBus
{
    public $memberMode = null;
    public $memberInfoMode = null;
    public $consignorReplenish= null;
    public $logisticsReplenish=null;
    public function __construct()
    {
        $this->memberMode = new Member();
        $this->memberInfoMode = new MemberInfo();
        $this->consignorReplenish = new ConsignorReplenish();
        $this->logisticsReplenish = new LogisticsReplenish();
    }

    /**增加货主企业
     * @param $data
     * @return mixed
     * @throws \think\Exception
     */
    public function addMember($data){
        $sessionDate = session(config('index.session_send_code'));
        //判断验证码，测试可以关闭
//        if(empty($sessionDate)){
//            throw new \think\Exception('验证码已经过期，请重新获取');
//        }
        if($sessionDate['phone'] != $data['phone']){
            throw new \think\Exception('手机号已更改，请重新获取');
        }
        if($sessionDate['code'] != $data['code']){
            throw new \think\Exception('手机验证码不正确，请重新填写');
        }
        $data['password'] = md5($data['password'].'_breeze');
        $result = $this->memberMode->addMember($data);
        if(empty($result)){
            throw new \think\Exception('添加失败，检查数据');
        }

        session(config('index.session_send_code'),'');
        return $data;
    }

    /**
     * 修改密码
     * @param $member_id
     * @param $password
     * @param $newPassword
     */
    public function resetPasswordBus($member_id,$password,$newPassword){
        $result = $this->memberMode->getMemberByWhere(['id'=>$member_id]);
        if($result['password'] != md5($password.'_breeze')){
            throw new \think\Exception('密码错误');
        }
        $where = [
            'id' => $member_id
        ];
        $data = [
            'password' => md5($newPassword.'_breeze')
        ];
        $res = $this->memberMode->updateMemberByWhere($where,$data);
        return $res;
    }
    public function loginBus($data){
        $where = [
            'phone'=>$data['phone'],

        ];
        $result = $this->memberMode->getMemberByWhere($where);
        if (empty($result) || $result->state == config('status.mysql.table_delete')){
            throw new \think\Exception('不存在该会员');
        }
        if($result['password'] != md5($data['password'].'_breeze')){
            throw new \think\Exception('密码错误');
        }
        $res = $this->memberMode->getMemberInfoById($result['id']);
        $setSessionData = [];
        if(empty($res->memberInfo)){
            $setSessionData = [
                'id'=>$result['id'],
                'phone'=>$result['phone'],
                "member_category" => '',
                "member_grade" => "0"
            ];
        }else{
            if($res->memberInfo['member_grade'] == 2){
                $whereTime = [
                    'member_id'=>$result['id']
                ];
                $dataTime = [
                    'member_grade' => 1
                ];
                if($res->memberInfo['member_time']){
                    if(strtotime($res->memberInfo['member_time'])<time()){
                        $this->memberInfoMode->changeMemberInfoFiled($whereTime,$dataTime);
                    }
                }else{
                    $this->memberInfoMode->changeMemberInfoFiled($whereTime,$dataTime);
                }
            }
            $res = $this->memberMode->getMemberInfoById($result['id']);
//            strtotime($member_time)<time()
            $setSessionData=[
                'id'=>$result['id'],
                'phone'=>$result['phone'],
                "member_category" => $res->memberInfo['member_category'],
                "status" => $res->memberInfo['status'],
                "member_grade" => $res->memberInfo['member_grade']
            ];
        }

        session(config('index.session_member'),$setSessionData);
        return $res;
    }

    public function getMemberWhere($data){
        $where = $data;
        $result = $this->memberMode->getMemberByWhere($where);
        return $result;
    }
    public function getMemberLogisticsById($data){
        $where = $data;
        $result = $this->memberMode->getMemberLogisticsById($where);
        return $result;
    }
    public function getMemberConsignorById($data){
        $where = $data;
        $result = $this->memberMode->getMemberConsignorById($where);
        return $result;
    }
    public function addMemberInfo($phone,$memberInfo,$supplement){
        if (empty($phone)){
            throw new \think\Exception('请先登录再添加');
        }
        $phone = [
            "phone" => $phone
        ];
        //判断memberInfo中是否有这个会员
        $result = $this->memberMode->getMemberByWhere($phone);
        if(empty($result) || $result->state == config('status.mysql.table_delete')){
            throw new \think\Exception('该会员已注销');
        }
        $memberId = $result->id;
        $foreignKey = [
            'member_id' => $memberId
        ];
        $resultMemberInfo = $this->memberInfoMode->getMemberInfoById($foreignKey);
        $resultConsignorReplenish = $this->consignorReplenish->getReplenishById($foreignKey);
        if(!empty($resultMemberInfo)){
            $addMemberInfo = $this->memberMode->updateMemberInfo($memberId,$memberInfo);
            if(!empty($addMemberInfo)){
                if(empty($resultConsignorReplenish)){
                    $this->memberMode->addConsignorReplenish($memberId,$supplement);
                }else{
                    $this->memberMode->updateConsignorReplenish($memberId,$supplement);
                }
            }
        }else{
            $addMemberInfo = $this->memberMode->addMemberInfo($memberId,$memberInfo);
            if(!empty($addMemberInfo)){
                if(empty($resultConsignorReplenish)){
                    $this->memberMode->addConsignorReplenish($memberId,$supplement);
                }else{
                    $this->memberMode->updateConsignorReplenish($memberId,$supplement);
                }
            }
        }
        $setSessionData=[
            "status" => 0,
        ];
        session(config('index.session_member'),$setSessionData);
        return $addMemberInfo;
    }

    public function addLogisticsInfo($phone,$memberInfo,$supplement){
        if (empty($phone)){
            throw new \think\Exception('请先登录在添加');
        }
        $phone = [
            "phone" => $phone
        ];
        //判断memberInfo中是否有这个会员
        $result = $this->memberMode->getMemberByWhere($phone);
        if(empty($result) || $result->state == config('status.mysql.table_delete')){
            throw new \think\Exception('该会员已注销');
        }
        $memberId = $result->id;
        $foreignKey = [
            'member_id' => $memberId
        ];
        $resultMemberInfo = $this->memberInfoMode->getMemberInfoById($foreignKey);
        $logisticsReplenish = $this->logisticsReplenish->getReplenishById($foreignKey);
        if(!empty($resultMemberInfo)){
            $addMemberInfo = $this->memberMode->updateMemberInfo($memberId,$memberInfo);
            if(!empty($addMemberInfo)){
                if(empty($logisticsReplenish)){
                    $this->memberMode->addLogisticsReplenish($memberId,$supplement);
                }else{
                    $this->memberMode->updateLogisticsReplenish($memberId,$supplement);
                }
            }
        }else{
            $addMemberInfo = $this->memberMode->addMemberInfo($memberId,$memberInfo);
            if(!empty($addMemberInfo)){
                if(empty($logisticsReplenish)){
                    $this->memberMode->addLogisticsReplenish($memberId,$supplement);
                }else{
                    $this->memberMode->updateLogisticsReplenish($memberId,$supplement);
                }
            }
        }
        $setSessionData=[
            "status" => 0,
        ];
        session(config('index.session_member'),$setSessionData);
        return $addMemberInfo;
    }

    public function changePasswordCheckBus($data){
        $where = [
            'phone' => $data['phone']
        ];
        $sessionDate = session(config('index.password_change_code'));

        if($sessionDate['phone'] != $data['phone']){
            throw new \think\Exception('手机号已更改，请重新获取');
        }
        if($sessionDate['code'] != $data['code']){
            throw new \think\Exception('手机验证码不正确，请重新填写');
        }

        $result = $this->memberMode->getMemberByWhere($where);
        if(empty($result)){
            throw new \think\Exception('不存在该用户');
        }
        session('changePasswordUser',$data);
//        $res = $this->memberMode->updateMemberByWhere($where,$data);
        return $result;
    }
    public function updatePasswordBus($data){
        $sessionDate = session('changePasswordUser');
        if($sessionDate && $sessionDate['phone'] == ''){
            throw new \think\Exception('验证码过期，请返回重新获取');
        }
        $where = [
            'phone'=>$sessionDate['phone']
        ];
        $data['password'] = md5($data['password'].'_breeze');
        $result = $this->memberMode->updateMemberByWhere($where,$data);
        if(empty($result)){
            throw new \think\Exception('修改失败，请重新操作');
        }
        return $result;
    }
}